<?php
namespace app\voice\controller;

use think\Db;
use think\Validate;

/**
 * 前台首页控制器
 * @package app\api\controller
 */
class Config extends Auth
{
    public $noNeedLogin = [
        'get_work_type'
    ];

    /**
     * 获取水吧配置信息
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $result = Db::name('qt_voice_config')->where('store_id', $this->userInfo['store_id'])->find();
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求方式', '', 204);
    }


    /**
     * 保存水吧配置信息
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = [
                'id|ID' => 'require|number',
                'hidden|隐藏请假技师' => 'require|number',
                'no_work|未上班技师' => 'require',
                'room_wait|显示房间等待信息' => 'require|number',
                'call|显示排钟信息 ' => 'require|number',
                'is_show_card|多种牌是否显示主牌' => 'require',
                'work_type|子工种和父级工种合并显示' => 'require|number',
                'adjunct|副项播报' => 'require|number',
                'wait_hidden|显示等待列表' => 'require',
                'play_number|播报次数' => 'require',
                'play_speed|播放速度' => 'require',
                //'noplay_work_type|不接受播报的工种分类' => 'require',
            ];
            // 定义验证提示
            $message = [
                'id.number' => 'ID必须为数字',
                'hidden.require' => '请输入隐藏确认信息',
                'hidden.number' => '隐藏确认信息格式不正确',
                'play_number.require' => '播报次数不能为空',
                'play_number.number' => '播报次数必须为数字',
                'play_speed.require' => '播放速度不能为空',
                'play_speed.number' => '播放速度必须为数字',
                //'noplay_work_type.require' => '不播报工种必须为数字',
                //'noplay_work_type.number' => '不播报工种必须为数字',
                'is_show_card.require' => '是否显示主牌不能为空',
            ];

            $validate = new Validate($rule, $message);
            if (!$validate->check($data)) {
                $this->result($validate->getError(), '', 204);
            }
            $update = Db::name('qt_voice_config')->where('store_id', $this->userInfo['store_id'])->update($data);
            if ($update !== false) {
                $this->result('保存成功', '', 200);
            }
            $this->result('保存失败', '', 204);
        }
        $this->result('无效的请求方式', '', 204);
    }

    /**
     * 获取用户工种
     */
    public function get_work_type(){

        if ($this->request->isPost()) {

            $level = $this->request->post('level');
            $where['store_id'] = $this->userInfo['store_id'] ?? 1;
            if (isset($level) && $level != '') {
                $where['pid'] = intval($level);
            }
            $list = Db::name('qt_work_type')
                ->field('id,title,pid')
                ->where($where)
                ->order('pid asc')
                ->select();

            $this->result('获取成功', $list,200);
        } else {
            $this->result('请求错误', '', 204);
        }
    }


}
